package com.sxx.beautifulcity.adapter;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.entity.Tuan_list_NearPayAtshopData;
import com.sxx.beautifulcity.entity.Tuan_list_ResultData;
import com.sxx.beautifulcity.entity.Tuan_list_Tongyong;
import com.sxx.beautifulcity.entity.Tuan_list_WifiData;
import com.sxx.beautifulcity.listenerInterface.OnMyItemClickListener;


import java.util.List;

/**
 * Created by dllo on 15/10/23.
 */
public class NearRecyclerViewAdapter extends RecyclerView.Adapter<NearRecyclerViewAdapter.NearRecyclerViewAdapterViewHolder> {
    private List<Tuan_list_ResultData> listResultDatas;
    private List<Tuan_list_NearPayAtshopData> listShopList;
    private List<Tuan_list_Tongyong> list;
    private List<Tuan_list_WifiData> datas;
    private OnMyItemClickListener listener;

    /***
     * 设置监听器对象
     * @param l
     */
    public  void  setOnMyItemClickListener(OnMyItemClickListener l){
        listener = l;
    }


    public void addData(List<Tuan_list_ResultData> listResultDatas,
                        List<Tuan_list_NearPayAtshopData> listShopList ,
                        List<Tuan_list_Tongyong> list) {
        this.listResultDatas = listResultDatas;
        this.listShopList = listShopList;
        this.list = list;
        notifyDataSetChanged();
    }

    class NearRecyclerViewAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView resturunt_nameTv, arearTv, space, payText,
                payText_title , overShowTv , sigle_short_titleTv , tongyongTv , other_descsTv;
        private SimpleDraweeView restrunnt_simpleDv;
        private RelativeLayout payRelativeLayout;
        private int position;

        public NearRecyclerViewAdapterViewHolder(View itemView) {
            super(itemView);
            resturunt_nameTv = (TextView) itemView.findViewById(R.id.resturunt_nameTv);
            arearTv = (TextView) itemView.findViewById(R.id.arearTv);
            space = (TextView) itemView.findViewById(R.id.space);
            payText = (TextView) itemView.findViewById(R.id.payText);
            payText_title = (TextView) itemView.findViewById(R.id.payText_title);
            restrunnt_simpleDv = (SimpleDraweeView) itemView.findViewById(R.id.restrunnt_simpleDv);
            payRelativeLayout = (RelativeLayout) itemView.findViewById(R.id.payRelativeLayout);
            payRelativeLayout.setOnClickListener(this);
        }

        /***
         * 系统点击事件的回调方法
         * @param v
         */
        @Override
        public void onClick(View v) {

            /***
             * 1.获得当前item行所对应的数据
             * 2.触发自定义监听事件监听器  并将数据回调给监听器
             */
            if (list != null && list.size() > 0) {
                Tuan_list_Tongyong data = list.get(position);
                if (listener != null) {
                    listener.onMyItemClick(data);
                }
            }
        }
    }

    @Override
    public NearRecyclerViewAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recyclerview_near_item, null);
        return new NearRecyclerViewAdapterViewHolder(view);
    }

    @Override
    public void onBindViewHolder(NearRecyclerViewAdapterViewHolder holder, int position) {
        if (listResultDatas != null && listResultDatas.size() > 0) {
            Tuan_list_ResultData result = listResultDatas.get(position);
            holder.arearTv.setText(result.getBizarea_title());
            holder.resturunt_nameTv.setText(result.getPoi_name());
            holder.space.setText(result.getPoi_distance());
            if (result.getPoi_image() != null) {
                String path = result.getPoi_image();
                requestFrescoImage(path, holder.restrunnt_simpleDv);
            }
        }
        holder.position = position;//获得行监听的下标
    }

    public void requestFrescoImage(String path, SimpleDraweeView draweeView) {
        Uri uri = Uri.parse(path);
        draweeView.setImageURI(uri);
    }

    @Override
    public int getItemCount() {
        return listResultDatas != null && listResultDatas.size() > 0 ? listResultDatas.size() : 0;
    }
}
